<template>
  <div>
    <el-button type="danger" @click="openDialog">添加</el-button>
    <!-- 全部打开：default-expand-all -->
    <el-table
      :data="tableData"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="id" label="ID"></el-table-column>
      <el-table-column prop="specsname" label="属性名称"></el-table-column>
      <el-table-column prop="attrs" label="属性值">
        <template slot-scope="scope">
          <el-tag v-for="(item,index) in scope.row.attrs" :key="index" type="success" size="medium">{{item}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status==1" type="success" size="medium">已启用</el-tag>
          <el-tag v-else type="danger" size="medium">已禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            circle
            size="mini"
            @click="edit(scope.row.id)"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            circle
            size="mini"
            @click="del(scope.row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page.sync="currentPage"
      :page-size="pageSize"
      layout="prev, pager, next, jumper,total"
      :total="total"
    ></el-pagination>
    <el-dialog :title="popType+'商品规格'" :visible.sync="dialogVisible" width="45%">
      <spec-info :transData="transData" :popType="popType"></spec-info>
      <span slot="footer" class="dialog-footer">
        <el-button size="medium" @click="dialogVisible = false">取 消</el-button>
        <el-button type="danger" size="medium" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import specInfo from "./info";
import { getSpecsNumber, getSpecsAttr, getSpecsList } from "../../request";
export default {
  data() {
    return {
      tableData: [],
      dialogVisible: false,
      popType: "新增",
      transData: [],
      currentPage: 1,
      pageSize: 2,
      total: 0
    };
  },
  components: {
    specInfo
  },
  created() {
    // 获取总条数
    this.getSpecsNum();
  },
  methods: {
    // 获取总条数
    async getSpecsNum() {
      let res = await getSpecsNumber();
      this.total = res[0].total;
      this.getData();
    },
    // 获取商品规格列表
    async getData() {
      let res = await getSpecsList({
        size: this.pageSize,
        page: this.currentPage
      });
      this.tableData = res;
    },

    openDialog() {
      this.popType = "新增";
      this.transData = {
        specsname: "",
        attrs: [""],
        status: 1
      };
      // };
      this.dialogVisible = true;
    },
    async edit(id) {
      this.popType = "修改";
      let res = await getSpecsAttr(id);
      this.transData.id=id;
      this.transData = res[0];
      this.dialogVisible = true;
    },
    del(id) {
      this.$confirm("确认删除吗,是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        confirmButtonClass: "el-button--danger"
      })
        .then(res => {
          this.doDel(id);
        })
        .catch(_ => {});
    },
    doDel(id) {
      this.$http.post("/specsdelete", { id }).then(res => {
        this.tableData = res.data.list;
      });
    },
    // 当前页
    handleCurrentChange(val) {
      this.getData();
    }
  }
};
</script>
<style lang="" >
.el-button {
  margin-bottom: 10px;
}
.el-pagination__total {
  float: right;
}
.el-tag{
  margin-left: 3px;
}
</style>